<template>
    <el-container>
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/heima.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type='info' @click='logout'>退出</el-button>
        </el-header>
        <!-- 页面主体区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ?'64px':'200px'">
                <!-- 侧边栏菜单区 -->
                <el-menu background-color="#444f5a" text-color="#fff" active-text-color="#409FFF" 
                :unique-opened='true' :collapse='isCollapse' :collapse-transition='false' :router='true' 
                :default-active='routerAddress'
                >
                    <div class="toggle-button" @click='toggleCollapse'>|||</div>
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.id+''" v-for="(item,inde) in menulist" :key='item.id'>
                        <!-- 一级菜单的模板区域 -->
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconobj[inde]"></i>
                            <!-- 文本 -->
                            <span>{{item.authName}}</span>
                        </template>

                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/'+it.path" v-for="it in item.children" :key='it.id' @click="address('/'+it.path)">
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 文本 -->
                                <span>{{it.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧内容主体 -->
            <el-main>
                <!-- 路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>

</template>

<script>
    export default {
        // 生命周期函数
        created() {
            this.getMenuList()
            this.routerAddress=window.sessionStorage.getItem('routerAddress')
        },
        name: 'Home',
        data() {
            return {
                // 左侧菜单数据
                menulist:[],
                // 一级菜单的图标
                iconobj:[
                // 对象写法 { 'id':xx }
                'iconfont icon-users',
                'iconfont icon-tijikongjian',
                'iconfont icon-shangpin',
                'iconfont icon-danju',
                'iconfont icon-baobiao',
                ],
                // 控制折叠
                isCollapse:false,
                // 存储 页面的路由地址 
                routerAddress:''
            }
        },
        methods: {
            // 退出按钮--跳转到登录页
            logout() {
                // 清空token
                window.sessionStorage.clear();
                // 跳转到登录页
                this.$router.push('/login')
            },
            // 获取所有的菜单数据（数据库）
            async getMenuList(){
                const {data:res} = await this.$http.get('menus')
                // console.log(res,1);
                if(res.meta.status!==200){
                    return this.$message.error(res.meta.msg)
                } else{
                    this.menulist = res.data
                }
            },
            // 点击 切换菜单的折叠与展开
            toggleCollapse(){
                this.isCollapse=!this.isCollapse
            },
            // 点击二级菜单 存储路由地址-保存链接的激活状态
            address(m){
                window.sessionStorage.setItem('routerAddress',m)
                // console.log(this.routerAddress);
                this.routerAddress=m
            }
        }
    }
</script>

<style scoped>
    .el-container {
        height: 100%;
    }

    .el-header {
        background-color: #3d4a50;
        display: flex;
        justify-content: space-between;
        padding-left: 0;
        align-items: center;
        color: #fff;
        font-size: 18px;
    }

    .el-header div {
        display: flex;
        align-items: center;
    }

    .el-header div span {
        margin-left: 15px;
    }

    .el-aside {
        background-color: #444f5a;
    }

    .el-main {
        background-color: #eaedf1;
    }
    
    .iconfont{
        margin-right: 10px;
    }

    .el-menu{
        border-right: none;
        /* box-sizing: border-box; 不行 */
    }

    .toggle-button{
        background-color: #4A5064;
        color: #fff;
        font-size: 11px;
        line-height: 24px;
        text-align: center;
        /* 字母间距 */
        letter-spacing: 0.2em;
        cursor: pointer;
    }
</style>